package com.yy.jhl.ui.manager.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yy.common.base.BaseAdapter;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.bean.CollectInfo;
import com.yy.jhl.bean.ProjectInfo;
import com.yy.jhl.interfaces.CommonCallBack;
import com.yy.jhl.ui.manager.activity.OrderDetailActivity;
import com.yy.jhl.ui.manager.activity.OrderFlowActivity;
import com.yy.jhl.util.DataRequestUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;


/**
 * Created by Ellis.Li on 2017/6/15.
 */

public class ProjectAdapter extends BaseAdapter {
    private List<ProjectInfo.DataBean> modelList;
    boolean isShowCollect;
    public ProjectAdapter(Context context,boolean isShowCollect) {
        super(context);
        this.isShowCollect = isShowCollect;
        modelList = new ArrayList<>();
    }
    @Override
    public void removeItem(int position) {
        super.removeItem(position);
        modelList.remove(position);
        notifyDataSetChanged();
    }
    @Override
    public void clearData() {
        super.clearData();
        modelList.clear();
        notifyDataSetChanged();
    }
    public void addData(List<ProjectInfo.DataBean> monomerModels) {
        modelList.addAll(monomerModels);
        notifyDataSetChanged();
    }
    public void refreshByProjectId(String projectId,String relateId){
        for (int i=0,size = modelList.size();i<size;i++){
            if (projectId.equals(modelList.get(i).getProject_id())){
                ProjectInfo.DataBean item = modelList.get(i);
                item.setRelated(relateId);
                modelList.remove(i);
                modelList.add(i,item);
                notifyDataSetChanged();
                break;
            }
        }
    }
    public void removeByProjectId(String projectId){
        for (int i=0,size = modelList.size();i<size;i++){
            if (projectId.equals(modelList.get(i).getProject_id())){
                modelList.remove(i);
                notifyDataSetChanged();
                break;
            }
        }
    }

    public void addAllData(List<ProjectInfo.DataBean> monomerModels) {
        clearData();
        modelList.addAll(monomerModels);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return modelList.size();
    }

    @Override
    public Object getItem(int i) {
        return modelList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final Holder storeHolder;
        if(view == null){
            storeHolder = new Holder();
            view = layoutInflater.inflate(R.layout.item_project,null);
            storeHolder.initView(view);
            view.setTag(storeHolder);
        }else{
            storeHolder = (Holder)view.getTag();
        }
        final ProjectInfo.DataBean itemModel = modelList.get(i);
        final boolean[] isCollected = {false};
        storeHolder.project_name_text.setText(itemModel.getProject_name());
        storeHolder.project_time_text.setText(itemModel.getUpdate_time());
        if ("1".equals(itemModel.getRelated())){
            isCollected[0] = true;
            storeHolder.collect_img.setImageResource(R.mipmap.blue_star);
        }else {
            isCollected[0] = false;
            storeHolder.collect_img.setImageResource(R.mipmap.gray_star);
        }
        if (isShowCollect){
            isCollected[0] = true;
            storeHolder.collect_img.setImageResource(R.mipmap.blue_star);
            storeHolder.collect_img.setVisibility(View.VISIBLE);
        }else {
            storeHolder.collect_img.setVisibility(View.GONE);
        }
        storeHolder.collect_img.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                //只要取消收藏
                DataRequestUtil.getInstance(mContext).collectProject(itemModel.getProject_id(), isCollected[0], new CommonCallBack() {
                    @Override
                    public void getCallBack() {
                        if (isCollected[0]){
//                            modelList.remove(i);
//                            notifyDataSetChanged();
                            CollectInfo collectInfo = new CollectInfo();
                            collectInfo.setProjectId(itemModel.getProject_id());
                            collectInfo.setRelateId("0");
                            EventBus.getDefault().post(collectInfo);
                            isCollected[0] = false;
                            storeHolder.collect_img.setImageResource(R.mipmap.gray_star);
                        }else {
                            isCollected[0] = true;
                            storeHolder.collect_img.setImageResource(R.mipmap.blue_star);
                        }
                    }
                });
            }
        });
        return view;
    }
    /**
     *
     */
    class Holder{
        private TextView project_name_text;
        private TextView project_time_text;
        private ImageView collect_img;
        void initView(View view) {
            project_name_text = (TextView) view.findViewById(R.id.project_name_text);
            project_time_text = (TextView) view.findViewById(R.id.project_time_text);
            collect_img = (ImageView) view.findViewById(R.id.collect_img);
        }

    }
}
